﻿


function LoadAudio(src) {

    //A hack to get around ioS audio loading restrictions
    //On the first touch of the user, we start playing the track to load the file.
    //Then we immediately pause the playback so no sound is heard
    var userAgent = window.navigator.userAgent;
    var click = document.onclick;
    if (click === undefined) {
        click = "touchstart";
    }
    else {
        var click = "click";
    }

    var audio = document.createElement('audio');
    audio.src = src;
    audio.autobuffer = true;
    audio.load();
    audio.muted = false;
    console.log("ua test 1: " + (userAgent.match(/iPad/i) || userAgent.match(/iPhone/i)));
    if (userAgent.match(/iPad/i) || userAgent.match(/iPhone/i)) {
        
        $("#intro-modal").attr('style', 'display:inline');
        var pauseAudio = function () {
            audio.pause();
            audio.removeEventListener('play', pauseAudio, false);
        }

        var startLoad = function () {
            audio.play();
            document.removeEventListener(click, startLoad, false);
            $("#modal-loading-icon").attr('style', 'display:inline');
            $(".modal-footer p").attr('style', 'display:none');
        }

        var dismissModal = function () {
        $("#intro-modal").attr('style', 'display:none');
        }

        audio.addEventListener('play', pauseAudio, false);

        $("#intro-modal").bind(click, function () {
            startLoad();
        });

        audio.addEventListener('canplaythrough', dismissModal, false);
    }

    this.audio = audio;

}


var playSound = function (soundClip) {
    //Each clip must be seperated by at least one second
    try {
        var spriteData = {
            chaBoy: {
                start: 0.0,
                end: 1.0
            },
            buhDuhDuh: {
                start: 2.0,
                end: 3.2
            },
            itsAGrounder: {
                start: 4,
                end: 5
            },
            turnTwo: {
                start: 6,
                end: 7
            },

            youCrazyDevon: {
                start: 8,
                end: 9.4
            },

            imGay: {
                start: 10,
                end: 12.3
            },

            helloMonica: {
                start: 13,
                end: 15.4
            },

            damn: {
                start: 16,
                end: 16.4
            },

            turnDownForWhat: {
                start: 17,
                end: 18.4
            },

            damnSon: {
                start: 19,
                end: 21
            },

            trapWhatsHappening: {
                start: 22,
                end: 22.7
            },

            fuckWithYourBoys: {
                start: 24,
                end: 26
            },

            yourBoyJayz: {
                start: 27,
                end: 27.8
            },

            youCrazyJayz: {
                start: 29,
                end: 30.5
            },

            gunShot: {
                start: 31,
                end: 31.7
            },

            gunCock: {
                start: 32.5,
                end: 33.1
            }
        };
        var audioFile = this.audio;

        audioFile.currentTime = spriteData[soundClip].start;
        audioFile.play();


        var checkTime = function () {
            if ((this.currentTime >= spriteData[soundClip].end) &&
                (this.currentTime <= spriteData[soundClip].end + 0.5)) {
                this.pause();
                audioFile.removeEventListener('timeupdate', checkTime, false);
            }
        }

        audioFile.addEventListener('timeupdate', checkTime, false);
    }
    catch (err) {
        console.log(err);
    }
}

LoadAudio('sounds/trapComp.mp3')


